//
//  UIView+BorderLine.m
//  Btpin
//
//  Created by Vic on 2019/3/10.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "UIView+BorderLine.h"

@implementation UIView (BorderLine)


- (UIView *)borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth borderType:(BorderSideType)borderType {
    
    if (borderType == BorderSideTypeAll) {
        self.layer.borderWidth = borderWidth;
        self.layer.borderColor = borderColor.CGColor;
        return self;
    }
    
    if (borderType & BorderSideTypeLeft) {
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f) toPoint:CGPointMake(0.0f, self.frame.size.height) color:borderColor borderWidth:borderWidth]];
    }
    
    if (borderType & BorderSideTypeRight) {
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:borderColor borderWidth:borderWidth]];
    }
    
    if (borderType & BorderSideTypeTop) {
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.frame.size.width, 0.0f) color:borderColor borderWidth:borderWidth]];
    }
    
    if (borderType & BorderSideTypeBottom) {
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, self.frame.size.height) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:borderColor borderWidth:borderWidth]];
    }
    
    return self;
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    shapeLayer.path = bezierPath.CGPath;
    shapeLayer.lineWidth = borderWidth;
    return shapeLayer;
}

- (void)setShadowShadowColor:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowOffset:(CGSize)shadowOffset {
    //layer对子layer进行切割，为true后切割后，阴影就看不到了
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOpacity = shadowOpacity;
    self.layer.shadowRadius =  shadowRadius;
    self.layer.shadowOffset = shadowOffset;
    
    
    /*
    CGRect shadowRect;
    CGFloat originX = 0;
    CGFloat originY = 0;
    CGFloat originW = self.bounds.size.width;
    CGFloat originH = self.bounds.size.height;
    switch (shadowPathSide) {
        case ShadowPathSideTop: {
            shadowRect = CGRectMake(originX, originY - shadowPathWidth/2, originW, shadowPathWidth);
        } break;
        case ShadowPathSideBottom: {
            shadowRect = CGRectMake(originX, originH - shadowPathWidth/2, originW, shadowPathWidth);
        } break;
        case ShadowPathSideLeft: {
            shadowRect = CGRectMake(originX - shadowPathWidth/2, originY, shadowPathWidth, originH);
        } break;
        case ShadowPathSideRight: {
            shadowRect = CGRectMake(originW - shadowPathWidth/2, originY, shadowPathWidth, originH);
        } break;
        case ShadowPathSideNoTop: {
            shadowRect = CGRectMake(originX -shadowPathWidth/2, originY + 1, originW + shadowPathWidth, originH + shadowPathWidth/2);
        } break;
        case ShadowPathSideAll: {
            shadowRect = CGRectMake(originX - shadowPathWidth/2, originY - shadowPathWidth/2, originW + shadowPathWidth, originH + shadowPathWidth);
        } break;
    }
    
    UIBezierPath *path =[UIBezierPath bezierPathWithRect:shadowRect];
    self.layer.shadowPath = path.CGPath;
    */
}

+ (CALayer *)setCornerAndshadowForViewFrame:(CGRect)viewFrame cornerRadius:(CGFloat)cornerRadius backgroundColor:(UIColor *)backgroundColor shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius {
    CALayer *subLayer = [CALayer layer];
    subLayer.frame = viewFrame;
    subLayer.cornerRadius = cornerRadius;
    subLayer.backgroundColor = shadowColor.CGColor;
    subLayer.masksToBounds = NO;
    subLayer.shadowColor = shadowColor.CGColor; //shadowColor阴影颜色
    
    // x>0:向右，x<0:向左，x=0:左右偏移
    // y>0:向下，y<0:向上，y=0:上下偏移
    subLayer.shadowOffset = shadowOffset; //shadowOffset阴影偏移,x向右偏移3，y向下偏移2,这个跟shadowRadius配合使用
    subLayer.shadowOpacity = shadowOpacity?shadowOpacity:0;//阴影透明度，默认0
    subLayer.shadowRadius = shadowRadius?shadowRadius:3;//阴影半径，默认3
    
    return subLayer;
}

@end
